﻿using JXC.Application.Dtos;
using JXC.Core.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JXC.Core.Repository;
using JXC.Application.Dtos;
using AutoMapper;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace JXC.WebAPI.Controllers
{
    /// <summary>
    /// 采购计划 控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ProcurementPlanController : ControllerBase
    {
        private readonly IRepository<PlanDetail, Guid> _repository2;
        private readonly IRepository<CaigouPlan, Guid> _repository;
        private readonly IMapper _mapper;
        private readonly IReService _service;

        public ProcurementPlanController(
            IRepository<CaigouPlan, Guid> repository,
            IRepository<PlanDetail, Guid> repository2,
            IReService service,
            IMapper mapper
            )
        {
            _repository2 = repository2;
            _repository = repository;
            _mapper = mapper;
            _service = service;
        }
        /// <summary>
        /// 获取采购计划列表
        /// </summary>
        /// <returns></returns>
        // GET: api/<ProcurementPlanController>
        [HttpGet]
        public async Task<IActionResult> Get()
        {
            List<CaigouPlan> caigouPlanList = await _repository.GetAllListAsync();
            if (caigouPlanList == null)
            {
                return BadRequest("获取采购计划列表失败");
            }
            List<CaiGouPlanDto> CaigouPlanDtoList = _mapper.Map<List<CaiGouPlanDto>>(caigouPlanList);


            return Ok(CaigouPlanDtoList);
        }

        // GET api/<ProcurementPlanController>/5
        [HttpGet("{id}", Name = "GetCaiGouPlanById")]
        public async Task<IActionResult> Get(Guid id)
        {
            //CaigouPlan caigouPlan = await _repository.SingleAsync(p => p.Id == id);
            List<CaigouPlan> caigouPlan = await _repository.GetAllListAsync(p => p.Id == id);
            List<CaiGouPlanDto> caiGouPlanDtos = _mapper.Map<List<CaiGouPlanDto>>(caigouPlan);

            return Ok(caiGouPlanDtos);
        }

        /// <summary>
        /// 录入采购计划
        /// </summary>
        /// <param name="value"></param>
        // POST api/<ProcurementPlanController>
        [HttpPost]
        public async Task<IActionResult> Post([FromBody] CaiGouPlanDto caiGouPlanDto)
        {

            CaigouPlan caigouPlan = _mapper.Map<CaigouPlan>(caiGouPlanDto);
            caigouPlan.InDate = DateTime.Now;
            caigouPlan.ExaminerID = caigouPlan.OperatorID;
            caigouPlan.ExamineState = 1;
            var res = await _repository.InsertAsync(caigouPlan);
            if (res == null)
            {
                return BadRequest("录入采购计划失败");
            }
            List<PlanDetail> planDetails = _mapper.Map<List<PlanDetail>>(caiGouPlanDto.PlanDetailDtos);
            foreach (var item in planDetails)
            {
                item.CaigouPlanId = res.Id;
            }
            foreach (var item in planDetails)
            {
                var res2 = await _repository2.InsertAsync(item);
            }

            return CreatedAtRoute("GetCaiGouPlanById", new { id = res.Id }, res);

        }
        /// <summary>
        /// 获取已经通过审核的采购订单，包括详情
        /// </summary>
        /// <returns></returns>
        [Route("GetPlan")]
        [HttpGet]
        public async Task<IActionResult> GetPlan()
        {
            try
            {
                List<CaigouPlan> caigouPlans = await _repository.GetAllListAsync(p => p.ExamineState == 1);
                List<PlanDetailDto> planDetailDtos = new List<PlanDetailDto>();
                foreach (var caigouPlan in caigouPlans)
                {
                    List<PlanDetail> planDetails1 = await _service.GetPlanDetailsAndGoods(caigouPlan.Id);
                    List<PlanDetailDto> planDetailDtos1 = _mapper.Map<List<PlanDetailDto>>(planDetails1);
                    foreach (var planDetailDto in planDetailDtos1)
                    {
                        planDetailDtos.Add(planDetailDto);
                    }
                }
                return Ok(planDetailDtos);
            }
            catch (Exception)
            {

                throw;
            }
        }
        // PUT api/<ProcurementPlanController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<ProcurementPlanController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
